package org.sao.security.demo.async;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class QueueListener implements ApplicationListener<ContextRefreshedEvent> {
	

	@Autowired
	private MockQueue mockQueue;
	
	
	@Autowired
	private DeferredResultHolder deferredResultHolder;
	
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		// TODO Auto-generated method stub
		
		new Thread(() -> {	
			while(true){
			if(StringUtils.isNotBlank(mockQueue.getCompleteOrder())){
				String orderNumber = mockQueue.getCompleteOrder();
				logger.info("返回订单结果通知"+orderNumber);
				deferredResultHolder.getMap().get(orderNumber).setResult(" order success");
				mockQueue.setCompleteOrder(null);
			}else{
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
			}).start();
	}
	
	
	

}
